<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <!--      <template slot="menuLeft">-->
      <!--        <el-button type="danger"-->
      <!--                   size="small"-->
      <!--                   icon="el-icon-delete"-->
      <!--                   plain-->
      <!--                   @click="handleDelete">删 除-->
      <!--        </el-button>-->
      <!--      </template>-->

      <template slot="menuLeft">
        <el-button type="warning" icon="el-icon-back" size="small" @click.stop="goBack()">返回列表</el-button>
      </template>
    </avue-crud>
    <div class="custom-text" style="z-index: 999;font-size: 15px;color: #606266">{{ historyDataNum }}</div>
  </basic-container>
</template>

<script>
import {
  getList,
  add,
  update,
  remove,
} from "@/api/service/gradesList";
import {mapGetters} from "vuex";

export default {
  props: {
    csId: {
      type: String,
      default: '0'
    }, showMainView: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 260,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        menu: false,
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学生姓名",
            prop: "xsName",
            search: true,
            span: 12,
          },
          // {
          //   label: "学生姓名",
          //   prop: "xsId",
          //   rules: [{
          //     required: true,
          //     message: "请选择学生",
          //     trigger: "blur"
          //   }],
          //   type: "tree",
          //   span: 12,
          //   dicData: [],
          //   props: {
          //     label: "name",
          //     value: "id"
          //   },
          //   nodeClick: (data, node) => {
          //     console.log(data,node)
          //     this.form.xsXh = data.xsXh
          //   }
          // },
          {
            label: "学号",
            prop: "xsXh",
            disabled: true,
            span: 12,

            // hide: true,
            // display: false
          },
          {
            label: "课程",
            prop: "kcIds",
            // search: true,
            hide: true,
            rules: [{
              required: true,
              message: "请选择参加考试科目",
              trigger: "blur"
            }],
            type: "select",
            multiple: true,
            span: 12,
            dicData: [],
            props: {
              label: "name",
              value: "id"
            },
          },
          {
            label: "分数",
            prop: "fs",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入分数",
                trigger: "blur"
              }
            ]
          },
          {
            label: "班级排名",
            prop: "bjpm",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入班级排名",
                trigger: "blur"
              }
            ]
          },
          {
            label: "年级排名",
            prop: "njpm",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入年级排名",
                trigger: "blur"
              }
            ]
          },
          {
            label: "总分",
            prop: "zf",
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入总分",
                trigger: "blur"
              }
            ]
          },
        ],
      },
      historyDataNum: '',
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: false,//this.vaildData(this.permission.jkcsevaluate_add, false),
        viewBtn: true,//this.vaildData(this.permission.jkcsevaluate_view, true),
        delBtn: false,//this.vaildData(this.permission.jkcsevaluate_delete, true),
        editBtn: false,//this.vaildData(this.permission.jkcsevaluate_edit, true)
      };
    },
    jsId() {
      return this.JSuserInfo.js_Id
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    goBack() {
      this.showMainView();
    },
    rowSave(row, done, loading) {
      console.log(row)
      // row.id = row.id === '' ? '0' : row.id
      // row.mskt_id = this.csId;//课程id
      // row.tenant_id = '' //租户 id
      // row.update_user = '0'//this.jsId //修改人 id
      // row.create_user = '0'//this.jsId //创建人id
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      row.csId = this.csId;
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then(res => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.id = this.csId;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
      // //获取学生
      // getClassStudentSelect({bjId:'1687000090098806806'}).then(res => {
      //   const xsData = this.findObject(this.option.column, "xsId");
      //   xsData.dicData = res.data.data;
      // });
      // //获取课程
      // getTeacherCourseSelect({bjId:'1687000090098806806'}).then(res => {
      //   const kcData = this.findObject(this.option.column, "kcIds");
      //   kcData.dicData = res.data.data;
      // });
    }
  }
};
</script>

<style>
</style>
